import React, { PureComponent } from 'react';
import moment from 'moment';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { message, Upload } from 'antd';
import { SingleDateC, ModalC, PrimaryC, UploadC } from '../../../comps';
import style from './index.less';
import { exportFile } from '@/src/utils/commonFunc';
import { connect } from 'dva';
@Form.create()
@connect(({ shareOptionManageM, loading, userM: user }) => ({ shareOptionManageM, loading, user }))
export default class MarketValueUploadC extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      fileList: [], //存放文件
      fileName: '', //上传loading提示
    };
  }
  componentDidMount() {}

  UNSAFE_componentWillReceiveProps(nextProps) {}
  // 取消
  onCancel = () => {
    this.props.changeVisible(false);
  };
  export = () => {
    //模板下载
    const {
      user: { currentUser = {} },
    } = this.props;
    const ivUser = currentUser.userId;

    exportFile('gateway/portfolio-server/option/marketValue/download', { ivUser: ivUser });
  };
  //点击上传
  import = (e) => {
    // const { fileList } = this.state;
    const { dispatch, loading } = this.props;
    const formData = new FormData();
    // fileList.forEach((file) => {
    formData.append('file', e);
    // });
    if (loading.effects['shareOptionManageM/uploadFile'] == true) return; //有正在执行的任务不再请求，因为按钮由loading 会出现样式问题，故没用
    dispatch({
      type: 'shareOptionManageM/uploadFile',
      payload: {
        file: formData,
      },
      callback: (res) => {
        if (res) {
          if (res.code == 0) {
            message.success('上传成功');
          } else {
            message.error(res.msg || res.mesg || '上传失败');
          }
        } else {
          message.error('上传失败');
        }
        this.setState({
          fileName: '',
          fileList: [],
        });
      },
    });
  };
  //执行跑批
  runCreditMonitor = () => {
    const {
      dispatch,
      form: { getFieldValue },
      loading,
    } = this.props;
    const date = getFieldValue('date');
    if (!date) return; //日期未选择 不可执行跑批
    if (loading.effects['shareOptionManageM/runCreditMonitor'] == true) return; //有正在执行的任务不再请求，因为按钮由loading 会出现样式问题，故没用
    dispatch({
      type: 'shareOptionManageM/runCreditMonitor',
      payload: {
        date: date.format('YYYYMMDD'),
      },
      callback: (res) => {
        if (res) {
          if (res.code == 0) {
            message.success('执行成功');
          } else {
            message.error(res.msg || '执行失败');
          }
        } else {
          message.error('执行失败');
        }
      },
    });
  };
  render() {
    return (
      <ModalC title={'期权市值上传'} visible={this.props.visible} maskClosable={false} onCancel={this.onCancel} width={450} height={140} footer={null}>
        <div className={style.content}>
          <div>
            <div>模板下载: </div>
            <a onClick={this.export}>场外期权市值上传模板</a>
          </div>
          <div>
            <div>文件导入: </div>
            <div>
              <UploadC
                title={'导入'}
                type={2}
                accept='.xlsx,.xls'
                showUploadList={false}
                onUpload={(e) => this.import(e)}
                // onUpload
                style={{ display: 'inline' }}
              ></UploadC>
            </div>
          </div>
          <div>
            <div>授信监控跑批: </div>
            <div style={{ width: '222px' }}>
              <SingleDateC
                form={this.props.form} // form对象
                dataIndex='date' // Form识别的Item ID
                disableEnd={moment()}
              />
            </div>
            <PrimaryC title={'执行'} onClick={this.runCreditMonitor} disabled={!this.props.form.getFieldValue('date') == true} style={{ marginLeft: '10px' }} />
          </div>
        </div>
      </ModalC>
    );
  }
}
